package com.ghy.ic.mo.task;

import com.ghy.ic.mo.dao.BcsVehicleDeliveryInfo;
import com.ghy.ic.mo.utils.JodaTimeUtil;
import org.apache.log4j.Logger;

import java.util.List;

import static com.ghy.ic.mo.constant.BcsConstant.*;

/**
 * @author Y-Tree
 * @date 18-12-3 下午4:02
 */
public class WatchDogTask implements Runnable {

    private static final Logger LOGGER = Logger.getLogger(WatchDogTask.class);
    @Override
    public void run() {
        LOGGER.info("准备为空表添加15天的服务记录...");
        // 0. 查看表中是否有数据，如果有就跳过，否则就添加
        List<BcsVehicleDeliveryInfo> infoAll = BcsVehicleDeliveryInfo.getAll();
        if (!infoAll.isEmpty()){
            // 有数据存在，不用添加数据
            LOGGER.info("我就什么也不做, 就是这么任性!");
        }else {
            // 无数据，则添加数据
            // 1. 获取连续15天的日期
            List<String> continuousDate = JodaTimeUtil.getContinuousDate(15);
            // 2. 根据日期添加上午和下午的服务。
            for (String date : continuousDate){
                // 上午
                BcsVehicleDeliveryInfo forenoon = new BcsVehicleDeliveryInfo();
                forenoon.setTimeMark(FORENOON);
                forenoon.setDepartureDate(date);
                forenoon.setDepartureTime(FORENOON_TIME);
                if (!BcsVehicleDeliveryInfo.add(forenoon)){
                    LOGGER.error("添加上午的记录失败！");
                }

                // 下午
                BcsVehicleDeliveryInfo afternoon = new BcsVehicleDeliveryInfo();
                afternoon.setTimeMark(AFTERNOON);
                afternoon.setDepartureDate(date);
                afternoon.setDepartureTime(AFTERNOON_TIME);
                if (!BcsVehicleDeliveryInfo.add(afternoon)){
                    LOGGER.info("添加下午的记录失败！");
                }
            }
            LOGGER.info("一下子添加了15天的记录, 好累啊！");
        }
    }
}
